package com.zzyl.service.impl;

import ch.qos.logback.core.status.Status;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.User;
import com.zzyl.entity.UserRole;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.UserService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.NoProcessing;
import com.zzyl.vo.RoleVo;
import com.zzyl.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    RoleMapper roleMapper;

    @Override
    public PageResponse<UserVo> selectPageResponse(UserDto userDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if(!EmptyUtil.isNullOrEmpty(userDto.getDeptNo())){/*处理部门编号后面多余的0.然后可以找到它的儿子*/
            userDto.setDeptNo(NoProcessing.processString(userDto.getDeptNo()));/*处理编号后就可以找到子的分页*/
        }
        Page<User> user1 = userMapper.selectPage(userDto);/*查询部门和职位*/
        for (User user2 : user1) {
            Long id = user2.getId();
            Set<String> roleVoIds = userRoleMapper.selectRoleIdByUserId(id);//通过用户id查询角色
          if (roleVoIds.size() > 0) {
               user2.setRoleVoIds(roleVoIds);
                Set<String> roleLabels = roleMapper.selectRoleById(roleVoIds);//通过上面查询的角色id查询角色信息
                user2.setRoleLabels(roleLabels);
            } else {
                continue;
            }
       }
        PageResponse<UserVo> pageResponse = PageResponse.of(user1, UserVo.class);
        return pageResponse;

    }

    @Override/*用户添加*/
    public Boolean insertUser(UserDto userDto) {
        User user = BeanUtil.toBean(userDto, User.class);
        user.setDataState("0");
        user.setNickName(userDto.getRealName());
        user.setUsername(userDto.getEmail());
        String password = "1234";

        //BCrypt加密
        user.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));/*给密码加点盐*/
        userMapper.insertUser(user);/*基本信息插入*/

        List<UserRole> userRoleList = new ArrayList<>();/*因为还要绑定角色*/
        for (String roleVoId : user.getRoleVoIds()) {
            userRoleList.add(new UserRole(user.getId(), Long.parseLong(roleVoId), "0"));
        }
        userRoleMapper.insert(userRoleList);
        return true;
    }

    @Override/*启用禁用*/
    public void isEnable(Long id, String status) {
        userMapper.isEnable(id,status);
    }

    @Override/*删除用户*/
    @Transactional(rollbackFor = Exception.class)
    public void deleteUser(List<String> userIds) {
        userMapper.deleteUser(userIds);
        userRoleMapper.deleteUser(userIds);
    }

    @Override/*修改用户*/
    @Transactional(rollbackFor = Exception.class)
    public Object updateUser(UserDto userDto) {
        /*先删后加*/
        User user = BeanUtil.toBean(userDto, User.class);
        userMapper.updateUser(user);/*先修改基本信息*/
        /*下面是关联表先删再曾*/
        userRoleMapper.deleteUser(Arrays.asList(userDto.getId().toString()));/*因为上面是一个list集合,所以需要转化一下*/

        List<UserRole> userRoleList = new ArrayList<>();/*因为还要绑定角色*/
        for (String roleVoId : user.getRoleVoIds()) {
            userRoleList.add(new UserRole(user.getId(), Long.parseLong(roleVoId), "0"));
        }
        userRoleMapper.insert(userRoleList);

        return true;
    }
}
